//20175
var mymap = function () {
    var omap = {};
    var visibleArea;
    var map;
    var icon="/images/bike_red.png";
    omap.init = function () {
        var point_str = localStorage.getItem("centerPoint");
        var zoom_str = localStorage.getItem("zoom");
        var latlng = new qq.maps.LatLng(29.3546673416, 105.9371173382);
        var zoom = 18;
        if (point_str != null) {
            var ar = point_str.split(',');
            latlng = new qq.maps.LatLng(parseFloat(ar[0]), parseFloat(ar[1]));
        }
        if (zoom_str != null) {
            zoom = parseInt(zoom_str);
        }
        map = new qq.maps.Map(document.getElementById("container"), {
            // 地图的中心地理坐标。
            center: latlng,
            zoom: zoom,//缩放级别
        });
        visibleArea = map.getBounds();
        //设置地图地图中心点
        qq.maps.event.addListener(map, 'center_changed', function () {
            localStorage.setItem("centerPoint", map.getCenter());
        });
        //获取地图缩放比例
        qq.maps.event.addListener(map, 'zoom_changed', function () {
            localStorage.setItem("zoom", map.getZoom());
        });
        qq.maps.event.addListener(map, 'bounds_changed', function () {
            visibleArea = map.getBounds();

        });
    }
    //初始化区域
    omap.initArea = function () {
        var areaArray = [];
        var infoWin = new qq.maps.InfoWindow({
            map: map
        });
        var n = 0;
        var oldcnetr;
        var timer = setInterval(function () {
            var str2 = localStorage.getItem("centerPoint");
            if (oldcnetr == str2) {
                return;
            } else {
                oldcnetr = str2;
            }
            if (visibleArea !== null && visibleArea != undefined) {
                var data = {};
                data.minLat = visibleArea.lat.minY;
                data.maxLat = visibleArea.lat.maxY;
                data.minLng = visibleArea.lng.minX;
                data.maxLng = visibleArea.lng.maxX;
                $.ajax({
                    type: "GET",
                    url: '/Admin/Map/queryDeviceAreaAll',
                    data: data,// 你的formid
                    async: true,
                    error: function (request) {
                        layer.msg("获取停车区域失败");
                    }, success: function (msg) {
                        loadArea(msg,areaArray);
                    }
                });
            }
        }, 2000)

        function loadArea(msg,areaArray) {
            omap.clearOverlays(areaArray);
            for (var i = 0; i < msg.length; i++) {
                var aa = new qq.maps.LatLng(msg[i].lat, msg[i].lng);
                var cirle = new qq.maps.Circle({
                    center: aa,
                    radius: msg[i].radius,
                    map: map,
                    strokeWeight: 1,
                    id: msg[i].id,
                    title: msg[i].areaName
                });
                (function (n) {
                    qq.maps.event.addListener(cirle, 'click', function () {
                        infoWin.open();
                        infoWin.setContent('<div style="text-align:center;white-space:' +
                            'nowrap;margin:10px;">' + areaArray[n].title + '</div>');
                        infoWin.setPosition(areaArray[n].getCenter());
                    });
                })(i);
                areaArray.push(cirle);
            }
            localStorage.setItem("centerPoint", map.getCenter());
        }
    }
    //初始化车辆
    omap.initBike = function () {
        var bikeArray = [];

        var timer = setInterval(function () {
            if (visibleArea !== null && visibleArea != undefined) {
                var data = {};
                data.minLat = visibleArea.lat.minY;
                data.maxLat = visibleArea.lat.maxY;
                data.minLng = visibleArea.lng.minX;
                data.maxLng = visibleArea.lng.maxX;
                $.ajax({
                    type: "GET",
                    url: '/Admin/Map/queryDeviceAll',
                    data: data,// 你的formid
                    success: function (res) {
                        loadBike(res,bikeArray);
                    }
                });
            }
        }, 5000)
        var cleartime=setInterval(function () {
            omap.clearOverlays(bikeArray);
            bikeArray=[];
        },300000)
        var infoWin = new qq.maps.InfoWindow({
            map: map
        });
        function loadBike(msg,bikeArray) {

            if(msg)
            {
                for(var i in msg)
                {

                    var point;
                    if(msg[i].lat!="0.0000000")
                    {
                        point = new qq.maps.LatLng(msg[i].lat, msg[i].lng);
                    }else{
                        point = new qq.maps.LatLng(msg[i].gsmLat, msg[i].gsmLng);
                    }

                    doMarkBike(point,msg[i]);


                }
            }
        }

        function doMarkBike(point,item){
            qq.maps.convertor.translate(point,1, function (res) {
                //point = res[0];
                var bike=checkForIn(item,bikeArray);
                var msg=item;
                //alert(bike)
                if(bike==undefined)
                {
                    var anchor = new qq.maps.Point(6, 6),
                        size = new qq.maps.Size(44, 46),
                        origin = new qq.maps.Point(0, 0),
                        icon = new qq.maps.MarkerImage('/admin/img/bike_red - small.png', size, origin, anchor);
                    var marker = new qq.maps.Marker({
                        icon: icon,
                        map: map,
                        position: point,
                        rotation:15,
                        dataId:msg.id,
                        dataSpeed:msg.speed,
                        title:msg.udid.substring(10,msg.udid.length),
                        dataUdid:msg.udid
                    });
                    bikeArray.push(marker);
                    qq.maps.event.addListener(marker, 'click', function () {
                        var data={};
                        data.udid=this.dataUdid;
                        var speed=this.dataSpeed;
                        $.ajax({
                            cache: true,
                            type: "GET",
                            url: '/Admin/Map/querybikeLatest',
                            data: data,// 你的formid
                            async: false,
                            cache: true,
                            error: function (request) {
                                layer.msg("获取停车区域失败");
                            }, success: function (msg) {
                                if(msg.state)
                                {
                                    infoWin.setContent('<div style="text-align:left;white-space:' +
                                        'nowrap;margin:10px;">' +
                                        '<p>编号:'+msg.data.udid+'</p>' +
                                        '<p>电量:主'+msg.data.percent+'%  副'+msg.data.subbtPercent+'%</p>' +
                                        '<p>速度:'+speed+'/km</p>' +
                                        '<p>档位:'+msg.data.gear+'档</p>' +
                                        '<p>电池锁:'+(msg.data.evLock==0?"未锁":'已锁')+'</p>' +
                                        '<p>车锁:'+(msg.data.evKey==0?"未锁":'已锁')+'</p>' +
                                        '</div>');
                                }
                            }
                        });
                        infoWin.setPosition(this.getPosition());
                        infoWin.open();
                    });
                    //bikeArray.push(marker);
                }else{
                    /*  var angle=omap.getAngle(point.lng,point.lat,bike.getPosition().lng,bike.getPosition().lat);
                     console.log(angle);
                     if(angle!=undefined&& ! isNaN(angle))
                     {
                     bike.setRotation((angle+50)%360);
                     }*/

                    //移动车辆
                    /* console.log(point);*/
                    bike.dataSpeed=msg.speed;
                    console.log(msg.speed);
                    bike.moveTo(point,15);

                }




            })
        }
        function checkForIn(varr,bikeArray) {
            console.log(varr);
            console.log(bikeArray);
            if(bikeArray){
                for (i in bikeArray)
                {
                    if(bikeArray[i].dataId==varr.id)
                    {
                        return bikeArray[i];
                    }
                }
            }
        }
    }
    //清除覆盖物
    omap.clearOverlays = function clearOverlays(areaArray) {
        if (areaArray) {
            for (i in areaArray) {
                areaArray[i].setMap(null);
            }
        }
        areaArray = [];
    }
    //计算角度
    omap.getAngle=function (px,py,mx,my) {
        try{
            var x = Math.abs(px-mx);
            var y = Math.abs(py-my);
            var z = Math.sqrt(Math.pow(x,2)+Math.pow(y,2));
            var cos = y/z;
            var radina = Math.acos(cos);//用反三角函数求弧度
            var angle = Math.floor(180/(Math.PI/radina));//将弧度转换成角度
            return angle;
        }catch (e)
        { console.log(e);
            return undefined;
        }
    }
    return omap;
}